﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mock_ehr_web.MeasuresServiceReference;
using mock_ehr_web.Models;
using mock_ehr_web;
using Measure = mock_ehr_lib.Model.Quality.Measure;
using MeasureEvaluateResult = mock_ehr_lib.Model.Quality.MeasureEvaluateResult;

namespace mock_ehr_web.Controllers
{
    public class PatientController : Controller
    {
        public ActionResult Index(int ID)
        {
            var app = this.GetApp();
            var allPatients = mock_ehr_lib.MockEHRApplication.Instance.PatientRepository.GetAllPatients();
            app.CurrentPatient = allPatients.Where(p => p.ID == ID).FirstOrDefault();

            if (app.CurrentPatient == null)
            {
                return View("Error");
            }
            return View(app.CurrentPatient.Summary);
        }

        public ActionResult Alerts(int ID)
        {
            var allPatients = mock_ehr_lib.MockEHRApplication.Instance.PatientRepository.GetAllPatients();
            var patientSummary = allPatients.Where(p => p.ID == ID).Select(p => p.Summary).FirstOrDefault();
            if (patientSummary == null)
            {
                return View("Error");
            }
            return PartialView("AlertList", patientSummary.Alerts);
        }

        public ActionResult FamilyHistory(int ID)
        {
            var allPatients = mock_ehr_lib.MockEHRApplication.Instance.PatientRepository.GetAllPatients();
            var patientSummary = allPatients.Where(p => p.ID == ID).Select(p => p.Summary).FirstOrDefault();
            if (patientSummary == null)
            {
                return View("Error");
            }
            return PartialView("FamilyHistoryList", patientSummary.FamilyHistory);
        }

        public ActionResult Immunizations(int ID)
        {
            var allPatients = mock_ehr_lib.MockEHRApplication.Instance.PatientRepository.GetAllPatients();
            var patientSummary = allPatients.Where(p => p.ID == ID).Select(p => p.Summary).FirstOrDefault();
            if (patientSummary == null)
            {
                return View("Error");
            }
            return PartialView("ImmunizationList", patientSummary.Immunizations);
        }

        public ActionResult Problems(int ID)
        {
            var allPatients = mock_ehr_lib.MockEHRApplication.Instance.PatientRepository.GetAllPatients();
            var patientSummary = allPatients.Where(p => p.ID == ID).Select(p => p.Summary).FirstOrDefault();
            if (patientSummary == null)
            {
                return View("Error");
            }
            return PartialView("ProblemList", patientSummary.Problems);
        }

        public ActionResult History(int ID)
        {
            var allPatients = mock_ehr_lib.MockEHRApplication.Instance.PatientRepository.GetAllPatients();
            var patientSummary = allPatients.Where(p => p.ID == ID).Select(p => p.Summary).FirstOrDefault();
            if (patientSummary == null)
            {
                return View("Error");
            }
            return PartialView("History", patientSummary);
        }

        public ActionResult Results(int ID)
        {
            var allPatients = mock_ehr_lib.MockEHRApplication.Instance.PatientRepository.GetAllPatients();
            var patientSummary = allPatients.Where(p => p.ID == ID).Select(p => p.Summary).FirstOrDefault();
            if (patientSummary == null)
            {
                return View("Error");
            }
            return PartialView("Error");
        }



        public ActionResult SocialHistory(int ID)
        {
            var allPatients = mock_ehr_lib.MockEHRApplication.Instance.PatientRepository.GetAllPatients();
            var patientSummary = allPatients.Where(p => p.ID == ID).Select(p => p.Summary).FirstOrDefault();
            if (patientSummary == null)
            {
                return View("Error");
            }
            return PartialView("SocialHistoryList", patientSummary.SocialHistory);
        }

        public ActionResult Search()
        {
            return View(mock_ehr_lib.MockEHRApplication.Instance.PatientRepository.GetAllPatients());
        }

        public ActionResult Quality(int ID)
        {
            var app = this.GetApp();
            var allPatients = app.PatientRepository.GetAllPatients();
            var patient = allPatients.Where(p => p.ID == ID).FirstOrDefault();
            if (patient == null)
            {
                return View("Error");
            }
            var client = new MeasuresServiceClient();
            var ccd = patient.CCD;
            var hasSmokingData = patient.Summary.SocialHistory.Any(i => i.Description.ToLower().Contains("cigar"));
            var orders = app.OrderRepository.GetOrdersByPatientID(patient.ID);
            var hasSmokingCounsel = orders.Any(i => i.Description.ToLower().Contains("counsel"));
            var hasPneumoVax = orders.Any(i => i.Description.ToLower().Contains("pneum"));
            if(hasSmokingData) // has smoking data
            {
                if(hasSmokingCounsel)
                {
                    if (hasPneumoVax)
                    {
                        ccd = MockEhrSettings.ccdSmokingCounselPneumovax;
                    }else
                    {
                        ccd = MockEhrSettings.ccdSmokingCounsel;
                    }
                }else
                {
                    if(hasPneumoVax)
                    {
                        ccd = MockEhrSettings.ccdSmokingDataPneumovax;
                    }else
                    {
                        ccd = MockEhrSettings.ccdSmokingData;
                    }
                }
            }else if(hasPneumoVax)
            {
                ccd = MockEhrSettings.ccdPneumovax;
            }
            var measures = client.GetEligibleMeasuresByCCD(ccd);
            var model = new PatientQualityReport();
            model.Categories = client.GetMeasureCategories();
            if(measures.Length > 0)
            {
                var evaluations = client.EvaluateMeasures(ccd, measures.Select(i => i.NqfNo).ToArray());
                model.Items = from m in measures
                                   join e in evaluations on
                                       m.NqfNo equals e.NqfNo
                                   select new QualityItem { Measure = new Measure
                                                                          {
                                                                             Category = m.Category,
                                                                             Description = m.Description,
                                                                             EvidenceSummary = m.EvidenceSummary,
                                                                             ExclusionQuestions = m.ExclusionQuestions,
                                                                             Question = m.Question,
                                                                             NqfNo = m.NqfNo,
                                                                             InfoUrl = m.InfoUrl
                                                                          }, 
                                                            Result = new MeasureEvaluateResult
                                                                         {
                                                                             NqfNo = e.NqfNo,
                                                                             Evaluation = e.Evaluation,
                                                                             Message = e.Message
                                                                         }
                                   };
            }
            return PartialView("Quality", model);
        }
    }
}
